/**
 * handle controller manager web page requests.
 */
var controllerTable = require("../Redis/controllerTable.js");
var redis = require("../Redis/redis.js");
var gatewayTable = require("../Redis/gatewayTable.js");

function getControllersByGateway(req, res) {
	"use strict";
	var context = {};
	redis.setClient(context);

	var gwId = req.query.gwId;
	
	var controlllers = [];
	var count = 0;
	var cursor = 0;
	var current = 0;
	if (req.query) {
		if (req.query.cursor) {
			cursor = req.query.cursor;
		}

		if (req.query.current) {
			current = +req.query.current;
		}
	}
	
	context.beginMulti();
	gatewayTable.count_c(context, gwId, function(err, reply) {
		if (err) {
			console.error("count error:" + err);
		} else {
			count = +reply;
		}
	});

	gatewayTable.scan_c(context, gwId, cursor, 16, function(err, replies) {
		if (err) {
			console.error("scan error:" + err);
		}

		if (replies) {
			cursor = +replies[0];
			var cids = replies[1];
			if (cids) {
				var length = cids.length;
				current += length;
				for (var x = 0; x < length; ++x) {
					controlllers.push("" + cids[x]);
				}
			}
		}
	});

	context.endMulti(function(err) {

		if (err) {
			console.error("getControllers database error:" + err);
		}

		var navigatorHeader = "Total:" + count + ", current:" + current;
		var nextPage = "/getControllers";
		if (cursor !== 0) {
			nextPage += ("?cursor=" + cursor + "&current=" + current + "&gwId=" + gwId);
		}
		res.render("controllerMgr", {
			title : "Controller manager",
			"navigatorHeader" : navigatorHeader,
			"nextPage" : nextPage,
			footer:"All controllers in gateway:" + gwId,
			header:"All controllers in gateway:" + gwId,
			"controllers" : controlllers
		});
	});

}

exports.getControllers = function(req, res) {
	"use strict";
	if (req.query && req.query.gwId) {
		getControllersByGateway(req, res);
		return;
	}
	
	var context = {};
	redis.setClient(context);

	var controlllers = [];
	var count = 0;
	var cursor = 0;
	var current = 0;
	if (req.query) {
		if (req.query.cursor) {
			cursor = req.query.cursor;
		}

		if (req.query.current) {
			current = +req.query.current;
		}
	}

	context.beginMulti();
	controllerTable.count(context, function(err, reply) {
		if (err) {
			console.error("count error:" + err);
		} else {
			count = +reply;
		}
	});

	controllerTable.scan(context, cursor, 16, function(err, replies) {
		if (err) {
			console.error("scan error:" + err);
		}

		if (replies) {
			cursor = +replies[0];
			var cids = replies[1];
			if (cids) {
				var length = cids.length;
				current += length;
				for (var x = 0; x < length; ++x) {
					controlllers.push("" + cids[x]);
				}
			}
		}
	});

	context.endMulti(function(err) {

		if (err) {
			console.error("getControllers database error:" + err);
		}

		var navigatorHeader = "Total:" + count + ", current:" + current;
		var nextPage = "/getControllers";
		if (cursor !== 0) {
			nextPage += ("?cursor=" + cursor + "&current=" + current);
		}
		res.render("controllerMgr", {
			title : "Controller manager",
			"navigatorHeader" : navigatorHeader,
			"nextPage" : nextPage,
			footer:"All controllers in database",
			header:"All controllers in database",
			"controllers" : controlllers
		});
	});

};

exports.loadController = function(req, res) {
	"use strict";
	var context = {};
	redis.setClient(context);
	var cid = req.query.cid;
	var result = {};
	var fields = [ "devId", "gwId", "description", "logintime", "name" ];
	context.beginMulti();
	controllerTable.loadFields(context, cid, fields, function(err, replies) {
		if (err) {
			result.err = "" + err;
		}
		
		if (replies) {
			var controller = {};
			controller.devId = "" + replies[0];
			controller.gwId = "" + replies[1];
			controller.description = "" + replies[2];
			//controller.ports = JSON.parse("" + replies[3]);
			controller.logintime = "" + replies[3];
			if (replies[5]) {
				controller.name = "" + replies[4];
			} else {
				controller.name = "";
			}
			
			result.controller = controller;
		}
		
	});
	
	controllerTable.loadPorts(context, cid, function(err, replies){
		if (err) {
			result.err = "" + err;
		}
		
		result.controller.ports = [];
		
		for (var x in replies) {
			if (replies.hasOwnProperty(x)) {
				result.controller.ports.push(JSON.parse(replies[x]));
			}
		}
	});
	
	context.endMulti(function(err){
		if (err) {
			result.err = "" + err;
		}
		
		res.send(JSON.stringify(result));
	});
};
